﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class UserControls_Page : UCBase
{
    public UserControls_Page()
    {
        Name = "自定义分页";
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    #region 属性
    private int _pageSize;
    /// <summary>
    /// 每页显示行的数目
    /// </summary>
    public int PageSize
    {
        get { return this._pageSize; }
        set { this._pageSize = value; }
    }

    private string _tableName;
    /// <summary>
    /// 表名或视图名
    /// </summary>
    public string TableName
    {
        get { return this._tableName; }
        set { this._tableName = value; }
    }

    private int _pageIndex;
    /// <summary>
    /// 当前页
    /// </summary>
    public int PageIndex
    {
        get { return this._pageIndex; }
        set { this._pageIndex = value; }
    }

    private string _condition;
    /// <summary>
    /// 查询条件(注:要带where关键字)
    /// </summary>
    public string Condition
    {
        get { return this._condition; }
        set { this._condition = value; }
    }

    private int _type;
    /// <summary>
    /// 类型:1(gridview) 2(datalist)
    /// </summary>
    public int Type
    {
        get { return this._type; }
        set { this._type = value; }
    }

    private GridView _gridID;
    /// <summary>
    /// gridview对象名
    /// </summary>
    public GridView GridID
    {
        get { return this._gridID; }
        set { this._gridID = value; }
    }

    private DataList _datalistID;
    /// <summary>
    /// datalist对象名
    /// </summary>
    public DataList DataListID
    {
        get { return this._datalistID; }
        set { this._datalistID = value; }
    }

    private int _findType;
    public int FindType
    {
        get { return this._findType; }
        set { this._findType = value; }
    }

    private string _field1;
    public string Field1
    {
        get
        {

            return this._field1;
        }
        set { this._field1 = value; }
    }

    private string _field2;
    public string Field2
    {
        get
        {
            return this._field2;
        }
        set { this._field2 = value; }
    }

    private string _like1;
    public string Like1
    {
        get
        {
            return this._like1;
        }
        set { this._like1 = value; }
    }

    private string _like2;
    public string Like2
    {
        get
        {
            return this._like2;
        }
        set { this._like2 = value; }
    }

    #endregion

    #region 绑定数据

    private void BindData()
    {
        int currentPage = 1;        //当前页
        DataSet ds = new DataSet();

        try
        {
            this._pageIndex = Convert.ToInt32(Session["CurrentPage"].ToString());
            currentPage = Convert.ToInt32(Session["CurrentPage"].ToString());
        }
        catch
        {
            this._pageIndex = Convert.ToInt32(lbCurrentPage.Text);
            currentPage = Convert.ToInt32(lbCurrentPage.Text);
        }

        DAL.Procedures.P_PageChange(ref ds, this._findType, this._field1, this._field2, this._like1, this._like2, this._tableName, this._pageIndex, this._pageSize, this._condition);

        System.Data.SqlClient.SqlDataAdapter DataAdapter = new System.Data.SqlClient.SqlDataAdapter();
        System.Data.SqlClient.SqlConnection Conn = Shove.Database.MSSQL.CreateDataConnection();
        if (Conn.State != ConnectionState.Open)
        {
            Conn.Open();
        }
        System.Data.SqlClient.SqlCommand Comm = new System.Data.SqlClient.SqlCommand("P_PageChange", Conn);
        Comm.CommandType = CommandType.StoredProcedure;

        //添加输入查询参数、赋予值
        Comm.Parameters.Add("@Type", SqlDbType.Int);
        Comm.Parameters["@Type"].Value = _findType;

        Comm.Parameters.Add("@Field1", SqlDbType.VarChar);
        Comm.Parameters["@Field1"].Value = _field1;

        Comm.Parameters.Add("@Field2", SqlDbType.VarChar);
        Comm.Parameters["@Field2"].Value = _field2;

        Comm.Parameters.Add("@like1", SqlDbType.VarChar);
        Comm.Parameters["@like1"].Value = _like1;

        Comm.Parameters.Add("@like2", SqlDbType.VarChar);
        Comm.Parameters["@like2"].Value = _like2;

        Comm.Parameters.Add("@TableName", SqlDbType.VarChar);
        Comm.Parameters["@TableName"].Value = _tableName;

        Comm.Parameters.Add("@PageIndex", SqlDbType.Int);
        Comm.Parameters["@PageIndex"].Value = _pageIndex;

        Comm.Parameters.Add("@PageSize", SqlDbType.Int);
        Comm.Parameters["@PageSize"].Value = _pageSize;

        Comm.Parameters.Add("@Condition", SqlDbType.VarChar);
        Comm.Parameters["@Condition"].Value = _condition;

        ////添加输出参数
        //Comm.Parameters.Add("@CurrentPage", SqlDbType.Int);
        //Comm.Parameters["@CurrentPage"].Direction = ParameterDirection.Output;

        Comm.ExecuteNonQuery();
        DataAdapter.SelectCommand = Comm;

        if (ds != null)
        {
            DataAdapter.Fill(ds, "table");
        }

        lbCurrentPage.Text = currentPage.ToString();
        Session["CurrentPage"] = currentPage;

        if (ds == null || ds.Tables.Count == 0)
            return;

        if (this._type == 1)
        {
            this._gridID.DataSource = ds;
            this._gridID.DataBind();
        }
        else
        {
            this._datalistID.DataSource = ds;
            this._datalistID.DataBind();
        }

        lbSumRecord.Text = ds.Tables[1].Rows[0][0].ToString();
        if (lbSumRecord.Text == "0")
        {
            lbCurrentPage.Text = "0";
        }
        if (Convert.ToInt32(lbSumRecord.Text) % this._pageSize != 0)
        {
            lbSumPage.Text = Convert.ToString(Convert.ToInt32(lbSumRecord.Text) / this._pageSize + 1);
        }
        else
        {
            lbSumPage.Text = Convert.ToString(Convert.ToInt32(lbSumRecord.Text) / this._pageSize);
        }

        if (lbCurrentPage.Text == "1" && Shove._Convert.StrToInt(lbSumPage.Text, 0) > Shove._Convert.StrToInt(lbCurrentPage.Text, 0))//首页(但不止一页)
        {
            lbFirst.Enabled = false;
            lbDown.Enabled = true;
            lbUP.Enabled = false;
            lbLast.Enabled = true;
        }
        else if (lbCurrentPage.Text == lbSumPage.Text && Shove._Convert.StrToInt(lbSumPage.Text, 0) > 1)//尾页
        {
            lbFirst.Enabled = true;
            lbDown.Enabled = false;
            lbUP.Enabled = true;
            lbLast.Enabled = false;
        }
        else if (Shove._Convert.StrToInt(lbSumPage.Text, 0) < 2)//只有一页
        {
            lbFirst.Enabled = false;
            lbDown.Enabled = false;
            lbUP.Enabled = false;
            lbLast.Enabled = false;
        }
        else if (Shove._Convert.StrToInt(lbCurrentPage.Text, 0) > 1 && Shove._Convert.StrToInt(lbSumPage.Text, 0) > Shove._Convert.StrToInt(lbCurrentPage.Text, 0))//中间页
        {
            lbFirst.Enabled = true;
            lbDown.Enabled = true;
            lbUP.Enabled = true;
            lbLast.Enabled = true;
        }
    }

    #endregion

    /// <summary>
    /// 首页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbFirst_Click(object sender, EventArgs e)
    {
        Session["CurrentPage"] = 1;
        BindData();
    }

    /// <summary>
    /// 上一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbUP_Click(object sender, EventArgs e)
    {
        lbCurrentPage.Text = Convert.ToString(Convert.ToInt32(Session["CurrentPage"].ToString()) - 1);
        Session["CurrentPage"] = lbCurrentPage.Text;
        BindData();
    }

    /// <summary>
    /// 下一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbDown_Click(object sender, EventArgs e)
    {
        lbCurrentPage.Text = Convert.ToString(Convert.ToInt32(Session["CurrentPage"].ToString()) + 1);
        Session["CurrentPage"] = lbCurrentPage.Text;
        BindData();
    }

    /// <summary>
    /// 尾页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbLast_Click(object sender, EventArgs e)
    {
        Session["CurrentPage"] = lbSumPage.Text;
        BindData();
    }

    /// <summary>
    /// 跳转
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnGO_Click(object sender, EventArgs e)
    {
        Session["CurrentPage"] = txtPage.Text;
        BindData();
    }
}
